package com.blue.wechat.receive;

import java.util.Date;

import com.blue.core.dict.WechatResponseType;
import com.blue.wechat.receive.model.AbstractRequest;
import com.blue.wechat.receive.model.AbstractResponse;
import com.blue.wechat.receive.model.ImageResponse;
import com.blue.wechat.receive.model.MusicResponse;
import com.blue.wechat.receive.model.NewsResponse;
import com.blue.wechat.receive.model.TextResponse;
import com.blue.wechat.receive.model.VideoResponse;
import com.blue.wechat.receive.model.VoiceResponse;

/**
 * 微信消息工具类
 * 
 * @author zhengj
 * @since 1.0 2017年1月29日
 */
public class WechatReceiveUtil
{
	/**
	 * 微信消息返回
	 */
	public static final String SUCCESS = "success";
	
	private WechatReceiveUtil()
	{
	}
	
	/**
	 * 创建微信响应信息，并填充了主要字段
	 * 
	 * @param request 微信请求信息
	 * @param clazz 微信响应信息类
	 * @return 微信响应信息
	 */
	public static <T extends AbstractResponse> T createResponse(AbstractRequest request, Class<T> clazz)
	{
		T t = null;
		try
		{
			t = clazz.newInstance();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		if (t == null)
			return null;
		
		t.setFromUserName(request.getToUserName());
		t.setToUserName(request.getFromUserName());
		t.setCreateTime(new Date());
		t.setWechatId(request.getWechatId());
		t.setUserId(request.getUserId());
		t.setId(request.getId());
		WechatResponseType type = WechatResponseType.TEXT;
		if (clazz == TextResponse.class)
			type = WechatResponseType.TEXT;
		else if (clazz == ImageResponse.class)
			type = WechatResponseType.IMAGE;
		else if (clazz == MusicResponse.class)
			type = WechatResponseType.MUSIC;
		else if (clazz == NewsResponse.class)
			type = WechatResponseType.NEWS;
		else if (clazz == VideoResponse.class)
			type = WechatResponseType.VIDEO;
		else if (clazz == VoiceResponse.class)
			type = WechatResponseType.VOICE;
		t.setType(type);
		return t;
	}
	
}
